<template>
  <div style="height:100%;width:100%;position:relative;">
      <!-- 角色用户弹窗 -->
      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="当前角色关联用户" :visible.sync="dialogRoleUserTableVisible">
          <el-container style="height: 500px; border: 1px solid #eee">
              <el-main style="overflow: hidden;">
                  <el-input @input="autosearch" @keydown.native="searchkeydown" placeholder="用户代号 | 用户名称" prefix-icon="el-icon-search" ref="qcodeinput" style="width:300px;" v-model="qcode"></el-input>
              <!-- <el-button @click="btnok" icon="el-icon-circle-check" style="margin-left:10px;margin-top:3px;">确定</el-button> -->
                  <el-button style="float:right;margin-top:3px;" @click="refreshdata" icon="el-icon-refresh">刷新数据</el-button>
                  <vxe-grid :columns="userTableColumn"  :pager-config="userTablePage"  :data="userTableData"  border height="430px" show-header-overflow="tooltip" show-overflow="tooltip" highlight-current-row highlight-hover-row ref="ddlcommonTable" resizable size="mini" stripe style="margin-top:5px"></vxe-grid>
              </el-main>
          </el-container>
      </el-dialog>
      <div class="pagetop">
          <div class="search-title">快速查询</div>
          <el-input v-model.trim="txtSearch" clearable @input="btnQuery" placeholder="项目代码|名称" style="width:150px" :disabled="changeFlag"></el-input>
          <div class="search-title">样本库类别</div>
          <el-select v-model="selCodeno" filterable style="width:150px" @change="btnQuery" clearable :disabled="changeFlag">
              <!-- <el-option v-for="(item, index) in selLibrtypeOptions" :key="index" :value="item.codeno" :label="item.codena"></el-option> -->
              <el-option  v-for="(item, index) in $opt.sampleLibraryType" :v-if="item.value!='50'" :key="index" :value="item.value" :label="item.label"></el-option>
          </el-select>
          <div class="search-title">
              <el-radio v-model="radLibrstatus" label="" selected @change="btnQuery()" size="mini" :disabled="changeFlag">全部</el-radio>
              <el-radio v-model="radLibrstatus" label="1" @change="btnQuery()" size="mini" :disabled="changeFlag">停用</el-radio>
              <el-radio v-model="radLibrstatus" label="0" @change="btnQuery()" size="mini" :disabled="changeFlag">在用</el-radio>
          </div>
          <div class="top-deal">
              <el-button @click="btnQuery()" icon="el-icon-search" v-has="'PINQUERY'" type="primary">查询</el-button>

              <el-button icon="el-icon-plus" v-has="'PADD'" @click.native="btnAdd()" :disabled="changeFlag">新增</el-button>

              <el-button icon="el-icon-delete" v-has="'PDELETE'" @click.native="btnDel()" :disabled="changeFlag">删除</el-button>

              <el-button icon="el-icon-document" v-has="'PMODIFY'" @click.native="btnSave()" :disabled="!changeFlag">保存</el-button>

              <el-button icon="el-icon-document" v-has="'PMODIFY'" @click.native="btnCancel()" :disabled="!changeFlag">取消</el-button>
          </div>
      </div>
      <div class="pagecenter">
          <rm-layout-right splitcolor="#FFFFFF" movingcolor="#CCFFFF" :splitwidth=5 :fixsize='windowWidth'>
              <template v-slot:fix>
                  <rm-layout-right splitcolor="#FFFFFF" movingcolor="#CCFFFF" :splitwidth=5 :fixsize="windowWidth2">
                      <template v-slot:fix>
                          <rm-layout-top splitcolor="#FFFFFF" movingcolor="#CCFFFF" :splitwidth=0 :fixsize="26">
                              <template v-slot:fix>
                                  <div style="float:left;width:100%;height:26px;border-top: solid 1px #ccc;border-left: solid 1px #ccc;border-right: solid 1px #ccc;border-bottom: solid 0px #ccc;margin:0px 0px 0px 0px;">
                                      <div style="float:left;width: calc(100%);height:100%;text-align:center;background:#F5F5F5;border: solid 0px #ccc;line-height:26px;font-size:13px;">可添加角色
                                      </div>
                                      <!-- <div style="float:left;height:100%;width: calc(100%);text-align:center;background:#F5F5F5;border: solid 1px #ccc;line-height:26px;font-size:13px;">本角色用户
                                      </div> -->
                                  </div>
                              </template>
                              <rm-layout-top splitcolor="#FFFFFF" movingcolor="#CCFFFF" :splitwidth=0 :fixsize="windowWidthTop-26">
                                  <template v-slot:fix>
                                      <vxe-table  :data.sync="rightTopTableData" border fit style="border-bottom:solid 0px;" height="auto" header-align="center" align="center" highlight-current-row highlight-hover-row ref="xTableRightTop" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe :cell-class-name="CellClassName" @cell-dblclick="LibraryRoleHandle($event,'insert')" :row-class-name="RowClassName">
                                          <vxe-table-column field="rolename" title="角色名称" width="100px" header-align="center"></vxe-table-column>
                                      <!-- <vxe-table-column field="hospname" title="所属医院" width="120" header-align="center"></vxe-table-column> -->
                                          <vxe-table-column field="roleno" title="角色代号" width="80" header-align="center"></vxe-table-column>
                                          <vxe-table-column title="查看用户" width="80" align="center">
                                              <template v-slot="{row}">
                                                  <el-button style="width:50px;height:100%" icon="el-icon-search"  type="primary" @click="btnQueryRoleUser('dictionaryRole',row)" ></el-button>
                                              </template>
                                          </vxe-table-column>
                                      </vxe-table>
                                  </template>
                                  <rm-layout-top splitcolor="#FFFFFF" movingcolor="#CCFFFF" :splitwidth=0 :fixsize="26">
                                      <template v-slot:fix>
                                          <div style="float:left;width:100%;height:26px;border-left: solid 1px #ccc;border-right: solid 1px #ccc;margin:0px 0px 0px 0px;">
                                              <!-- <div style="float:left;width: calc(100%);height:100%;text-align:center;background:#F5F5F5;border: solid 1px #ccc;line-height:26px;font-size:13px;">可添加用户
                                              </div> -->
                                              <div style="float:left;height:100%;width: calc(100%);text-align:center;background:#F5F5F5;border: solid 0px #ccc;line-height:26px;font-size:13px;">样本库角色
                                              </div>
                                          </div>
                                      </template>
                                      <vxe-table  :data.sync="rightDownTableData" border fit height="auto" header-align="center" align="center" highlight-current-row highlight-hover-row ref="xTableRightDown" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe :cell-class-name="CellClassName" @cell-dblclick="LibraryRoleHandle($event,'delete')" :row-class-name="RowClassName">
                                          <vxe-table-column field="rolename" title="角色名称" width="100px" header-align="center"></vxe-table-column>
                                          <!-- <vxe-table-column field="hospname" title="所属医院" width="120" header-align="center"></vxe-table-column> -->
                                          <vxe-table-column field="roleno" title="角色代号" width="80" header-align="center"></vxe-table-column>
                                          <vxe-table-column  title="查看用户" width="80" align="center">
                                              <template v-slot="{row}">
                                                  <el-button style="width:50px;height:100%" icon="el-icon-search"  type="primary" @click="btnQueryRoleUser('currentRole',row)" ></el-button>
                                              </template>
                                          </vxe-table-column>
                                      </vxe-table>
                                  </rm-layout-top>
                              </rm-layout-top>
                          </rm-layout-top>
                      </template>
                      <div style="width:100%;float:left;height:100%;border:1px solid #ccc;overflow:auto;" id="showbox">
                          <el-form :inline="true" @submit.native.prevent label-width="auto" ref="form_bbjlx" :model="detailData">
                              <table style="width:100%;font-size:12px;white-space: nowrap; padding-top:3px;text-align: left; align:;center;padding:0px 5px 5px 0px">
                                  <tr>
                                      <td style="color:#F56C6C;text-align: right; ">样本库ID</td>
                                      <td colspan="3">
                                          <el-input style="width:200px;" v-model="detailData.librid" @input="changeFlag = true" ref="txtLibrID" :disabled="true"></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="color:#F56C6C;text-align: right; ">样本库代码</td>
                                      <td colspan="3">
                                          <el-input style="width:200px;" v-model="detailData.librcode" @input="changeFlag = true" ref="txtLibrcode" @keyup.enter.native="Autofocus('txtLibrcode')" clearable></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="color:#F56C6C;text-align: right; ">样本库名称</td>
                                      <td colspan="3">
                                          <el-input style="width:200px;" v-model="detailData.librname" @input="changeFlag = true" ref="txtLibrName" @keyup.enter.native="Autofocus('txtLibrName')" clearable></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="text-align: right; ">样本库类型</td>
                                      <td colspan="3">
                                          <el-select v-model="detailData.librtype" filterable style="width:200px" ref="selLibrType" @keyup.enter.native="Autofocus('selLibrType')" @change="changeFlag = true" clearable>
                                              <!-- <el-option v-for="(item,index) in selLibrtypeOptions" :key="index" :value="item.codeno" :label="item.codena"></el-option> -->
                                              <el-option  v-for="(item, index) in $opt.sampleLibraryType" :v-if="item.value!='50'" :key="index" :value="item.value" :label="item.label"></el-option>
                                          </el-select>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="color:#F56C6C;text-align: right; ">在用</td>
                                      <td colspan="3">
                                          <el-checkbox style="text-align:left" v-model="detailData.librstatus" true-label='0' false-label='1' @input="changeFlag = true" ref="txtLibrstatus" @keyup.enter.native="Autofocus('txtLibrstatus')"></el-checkbox>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="text-align: right; ">负责人</td>
                                      <td colspan="3">
                                          <el-input style="width:200px;" v-model="detailData.dutyuser" @input="changeFlag = true" ref="txtDutyuser" @keyup.enter.native="Autofocus('txtDutyuser')" clearable></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="text-align: right; ">联系电话</td>
                                      <td colspan="3">
                                          <el-input style="width:200px;" v-model="detailData.telnum" @input="changeFlag = true" ref="txtTelnum" @keyup.enter.native="Autofocus('txtTelnum')" clearable></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="text-align: right; ">创建人</td>
                                      <td colspan="3">
                                          <rm-ddl-common datatype="userinfo" dataname="用户" v-model="detailData.createuser" width="200px" :disabled="true"></rm-ddl-common>
                                      </td>

                                  </tr>
                                  <tr>
                                      <td style="text-align: right; ">创建时间</td>
                                      <td colspan="3">
                                          <el-input style="width:200px;" v-model="detailData.createdt" @input="changeFlag = true" ref="txtCreatedt" :disabled="true"></el-input>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td style="text-align: right; vertical-align:top">类型说明</td>
                                      <td colspan="3">
                                          <el-input :rows="4" style="width:200px;" type="textarea" v-model="detailData.librdesc" @input="changeFlag = true" ref="txtLibrdesc" @keyup.enter.native="Autofocus('txtLibrdesc')" clearable></el-input>
                                      </td>
                                  </tr>
                              </table>
                          </el-form>
                      </div>
                  </rm-layout-right>
              </template>
              <vxe-table :loading="bLoading" :data.sync="leftTableData" border fit height="auto" header-align="center" align="center" highlight-current-row highlight-hover-row ref="xTable" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe :cell-class-name="CellClassName" @current-change="CurrentChange" :row-class-name="RowClassName">
                  <vxe-table-column field="librid" title="样本库ID" width="60px" header-align="center" align="left"></vxe-table-column>
                  <vxe-table-column field="librcode" title="样本库代码" width="80px" header-align="center" align="left"></vxe-table-column>
                  <vxe-table-column field="librname" title="样本库名称" width="120px" header-align="center" align="left"></vxe-table-column>
                  <vxe-table-column field="librstatus" title="状态" width="45px"></vxe-table-column>
                  <vxe-table-column field="librtype" title="样本库类别" width="100px"></vxe-table-column>
                  <vxe-table-column field="dutyuser" title="负责人" width="80px"></vxe-table-column>
                  <vxe-table-column field="telnum" title="联系电话" width="120px"></vxe-table-column>
                  <vxe-table-column field="librdesc" title="样本库说明" width="160px" header-align="center" align="left"></vxe-table-column>
                  </vxe-table>
          </rm-layout-right>
      </div>
      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="提示" :visible.sync="bDelVisible" width="500px" height="300px">
          <span class="el-icon-info">确定删除<b>样本库:</b><b style="color:red">{{detailData.librname }} ({{detailData.librcode}})</b>的记录吗？</span>
          <span slot="footer" class="dialog-footer">
              <el-button type="primary" @click="btnDelComfirm">确 定</el-button>
              <el-button @click="btnDelCancel" :autofocus="true">取 消</el-button>
          </span>
      </el-dialog>
  </div>

</template>

<script type="text/javascript">
import { Promise } from "q"
import { invalid } from "moment"
import md5 from "js-md5"
import XEUtils from "xe-utils"
// XEUtils.mixin({
//     // 自定义全局的格式化处理函数
//     formatSelect(cellValue, list) {
//         console.log("list", list)
//         let item = list.find((item) => item.value == cellValue)
//         return item ? item.label : cellValue
//     },
// })
export default {
  name: "SMP130",
  data() {
      return {
          bLoading: false,
          bDelVisible:false,
          changeFlag: false,
          ifCanEditFlag: false,
          okFirstLoading: true,
          selflag: "1",
          radLibrstatus: "", //状态（0-在用；1-停用；空-全部）
          txtSearch: "", //查询内容（对 样本库代码、名称进行模糊查询）
          selCodeno: "", //样本库类型代码
          selCodena: "", //样本库类型名称
          librid: "", //主键 样本库ID
          actionindex: "M", //M默认 A新增 E修改 D删除
          detailData: {
              librid: null,
              librcode: "",
              librname: "",
              librstatus: 0,
              librtype: null,
              dutyuser: "",
              telnum: "",
              librdesc: "",
              createdt: "",
              createuser: "",
              librdesc: "",
          },
          oldDetailData: {
              librid: null,
              librcode: "",
              librname: "",
              librstatus: 0,
              librtype: null,
              dutyuser: "",
              telnum: "",
              librdesc: "",
              createdt: "",
              createuser: "",
              librdesc: "",
          },
          selRow: {
              librid: null,
              librcode: "",
              librname: "",
              librstatus: 0,
              librtype: null,
              dutyuser: "",
              telnum: "",
              librdesc: "",
              librdesc: "",
          },

          leftTableData: [],
          rightTopTableData:[],
          rightDownTableData:[],
          userTableData:[],//弹窗grid
          firstUserTableData:[],
          firstUser:false,//点击角色查看当前用户时初始化为false
          refreshFlag:'',
          addData: [],
          modData: [],
          selLibrtypeOptions: [], //样本库类型
          selroleRow:{
              librid:0,
              roleno:null
          },
          qcode: "",         //弹窗过滤watch监听变量
          dialogRoleUserTableVisible:false,//样本类型添加提示框
          userTableColumn:[],//用户弹窗Grid绑定列
          userTablePage: {       //分页大小
              pageSize: 1000,
          },
          windowWidth: document.documentElement.clientWidth/2-100,  //实时屏幕宽度
          windowWidth2: document.documentElement.clientWidth/5+60,   //实时屏幕宽度
          windowWidthTop:document.documentElement.clientHeight/2-50    //百分比高度
      }
  },
  conpute:{},//计算属性,监听变量被读时将进入该逻辑判断
  watch: {},
  created() {
      //this.ddlLibrtype() //初始化样本类型
      this.$nextTick(() => {
          this.btnQuery() //初始化主列表
      })
  },
  mounted() {},
  updated() {
      if (this.okFirstLoading) {
          let currentrow = this.$refs.xTable.getData(0)
          this.$nextTick(() => {
              if (currentrow) {
                  this.okFirstLoading = false
                  this.$refs.xTable.setCurrentRow(currentrow)
              }
          })
      }
  },
  methods: {
      /*-----------------------------行改变-----------------------------*/
      CurrentChange({ row, rowIndex }, event) {
          if (this.selflag == "1") {
              this.$nextTick(async () => {
                  await this.CurrentChangeEvent(row, rowIndex)
              })
          } else {
              this.$refs.xTable.setCurrentRow(this.selRow)
              return
          }
      },
      async CurrentChangeEvent(row, rowIndex) {
          this.librid = row.librid
          this.selflag == "0"
          this.CurrentSave()
              .then(() => {
                  this.selRow = row
                  this.librid = row.librid
                  this.$refs.xTable.setCurrentRow(row)
                  this.$nextTick(async () => {
                      await this.DetailQuery()
                      await this.RoleInfoQuery()
                      this.selflag = "1"
                  })
              })
              .catch((ex) => {
                  this.$refs.xTable.setCurrentRow(this.selRow)
                  this.selflag = "1"
              })
      },
      CurrentSave() {
          return new Promise((s, f) => {
              let haschange = false
              let saveinfo = XEUtils.clone(this.detailData, true)
              if (this.actionindex == "M") {
                  //如果是修改
                  if (!XEUtils.isEqualWith(this.oldDetailData, saveinfo)) {
                      //表单被更改
                      haschange = true
                  }
              } else if (this.actionindex == "A") {
                  //如果是新增
                  saveinfo.createuser = this.$tool.getSession(
                      this.$tool.loginuserno
                  )
                  saveinfo.createdt = this.$pub.InitDateTime()
                  haschange = true
              }
              if (saveinfo.librcode == "") {
                  this.$alert(
                      "样本库代码不可为空！",
                      "提示",
                      { type: "warning" },
                      {
                          confirmButtonText: "确定",
                      }
                  )
                  this.$refs.txtLibrcode.focus()
                  return false
              }
              if (saveinfo.librname == "") {
                  this.$alert(
                      "样本库名称不可为空！",
                      "提示",
                      { type: "warning" },
                      {
                          confirmButtonText: "确定",
                      }
                  )
                  this.$refs.txtLibrName.focus()
                  f()
                  return false
              }
              if (haschange) {
                  this.$nextTick(async () => {
                      const res = await this.$tool.request({
                          url: "SMP/SMP1/SMP130",
                          method: "post",
                          data: {
                              action: "smp_library_addedit",
                              data: saveinfo,
                          },
                      })
                      if (res.errcode == "1") {
                          this.$message({
                              message: "保存成功！",
                              type: "success",
                              showClose:true,
                          })
                          this.librid = res.keyid
                          this.changeFlag = false
                          //不是删除操作
                          if (this.actionindex == "A") {
                              //新增数据成功后的不刷新操作
                              saveinfo.librid = this.librid
                              this.detailData.librid = this.librid
                              if (saveinfo.librstatus == "0") {
                                  saveinfo.librstatus = "在用"
                              } else if (saveinfo.librstatus == "1") {
                                  saveinfo.librstatus = "停用"
                              }
                              this.$opt.sampleLibraryType.forEach((item) => {
                                  // if (saveinfo.librtype == item.codeno)
                                  //     saveinfo.librtype = item.codena
                                  if (saveinfo.librtype == item.value)
                                      saveinfo.librtype = item.label
                              })
                              this.$refs.xTable
                                  .insertAt(saveinfo, -1)
                                  .then(({ row }) => {
                                      this.$refs.xTable.setCurrentRow(row)
                                      this.selRow = row
                                  })
                              if (this.addData.indexOf(this.librid) < 0) {
                                  this.addData.push(this.librid)
                              }
                          } else if (this.actionindex == "M") {
                              this.setToTable(saveinfo)
                              if (this.modData.indexOf(this.librid) < 0) {
                                  this.modData.push(this.librid)
                              }
                          }
                          this.actionindex = "M"
                          s()
                      } else {
                              this.$message({
                              message: "保存失败！" + res.obj,
                              type: "error",
                              showClose:true,
                          })
                          f()
                      }
                  })
              } else {
                  this.changeFlag = false
                  s()
              }
          })
      },
      /*-----------------------------按钮类-----------------------------*/
      async btnQuery() {
          this.bLoading = true
          this.actionindex = "M"
          this.addData = []
          this.modData = []
          try {
              const res = await this.$tool.request({
                  url: "SMP/SMP1/SMP130",
                  method: "post",
                  data: {
                      action: "smp_library_query",
                      keystr: this.txtSearch,
                      librtype: this.selCodeno,
                      librstatus: this.radLibrstatus,
                  },
              })
              this.leftTableData = res.RData.Data
              this.leftTableData.forEach((item) => {
                  //在用状态转换
                  if (item.librstatus == 0) {
                      item.librstatus = "在用"
                  } else if (item.librstatus == 1) {
                      item.librstatus = "停用"
                  }
                  this.$opt.sampleLibraryType.forEach((item2) => {
                      if (item.librtype == item2.value) {
                          item.librtype = item2.label
                      }
                  })
              })

              this.okFirstLoading = true
              this.selRow = null
              if (res.RData.Data.length > 0) {
                  this.selRow = res.RData.Data[0]
                  this.librid = res.RData.Data[0].librid
              } else {
                  this.librid = ""
                  this.detailData = {}
              }
              this.bLoading = false
              if (this.librid != "") {
                  await this.DetailQuery()
              }
              await this.RoleInfoQuery()

              this.changeFlag = false
          } catch (e) {
              this.bLoading = false
              this.$message({
                  message: e.message,
                  type: "error",
                  showClose:true,
              })
          }
      },
      async DetailQuery() {
          try {
              const res = await this.$tool.request({
                  url: "SMP/SMP1/SMP130",
                  method: "post",
                  data: {
                      action: "smp_library_query_byid",
                      librID: this.librid,
                  },
              })
              this.detailData = res.Data[0]
              if (this.detailData.librstatus == 0) {
                  this.detailData.librstatus = "0"
              } else if (this.detailData.librstatus == 1) {
                  this.detailData.librstatus = "1"
              }
              //换行保存储备数据
              this.oldDetailData = XEUtils.clone(this.detailData, true)
          } catch (e) {
              this.$message({
                  message: e.message,
                  type: "warning",
                  showClose:true,
              })
          }
      },
      //角色信息查询
      async RoleInfoQuery()
      {
          var tmpLibrid = this.librid;
          try {
               if (tmpLibrid) {
               //QueryLibararyIDRoleInfo
                      const res = await this.$tool.request({
                      url: "SMP/SMP1/SMP130",
                      method: "post",
                      data: {
                          action: "QueryLibararyIDRoleInfo",
                          libraryId: this.librid,
                      },
                  })
                  // rightTopTableData:[],
                  // rightDownTableData:[],
                  if (res) {
                      console.log(res);
                      if (res.roleDictionary.Data) {
                          this.rightTopTableData = res.roleDictionary.Data;
                      }
                      if (res.libraryRole.Data) {
                          this.rightDownTableData = res.libraryRole.Data;
                      }
                  }
               }
               else
               {
                  //QueryRoleAndUserInfo
                   const res = await this.$tool.request({
                      url: "SMP/SMP1/SMP130",
                      method: "post",
                      data: {
                          action: "QueryRoleAndUserInfo",
                      },
                  })
                   if (res) {
                      if (res.Data) {
                          this.rightTopTableData = res.Data;
                          this.rightDownTableData = [];
                      }
                  }
               }

          } catch (e) {
               this.$message({
                  message: e.message,
                  type: "warning",
                  showClose:true,
              })
          }

      },
      //根据角色查用户
      async btnQueryRoleUser(strRole,row)
      {

          //打开角色用户弹窗
          this.dialogRoleUserTableVisible = true;
          //firstUser 每次打开用户弹窗初始化
          this.firstUser = true;
          this.qcode = '';
          let roleID;
          if (strRole=='dictionaryRole')
              roleID = row.roleno
          else if(strRole=='currentRole')
              roleID = row.roleno
          if (roleID) {
              this.refreshFlag = roleID;
              const res = await this.$tool.request({
                      url: "SMP/SMP1/SMP130",
                      method: "post",
                      data: {
                          action: "QueryRoleIdUser",
                          roleId: roleID,
                      },
                  })
              if (res) {
                  //数据源
                  if (res.Data) {
                      this.userTableData = res.Data
                  }
                  //绑定列
                  if (res.Cols) {
                      var collist = [];
                          for (let i = 0, len = res.Cols.length; i < len; i++) {
                              const item = res.Cols[i];
                              if (item.field != "hidkey" && item.field != "hidname" && item.field != "inputcode" && item.field != "shortmsgcode") {
                                  let colitem = {
                                      field: item.field,
                                      title: item.title,
                                      width: item.width,
                                  };
                              collist.push(colitem);
                          }
                      }
                      this.userTableColumn = collist;
                  }
              }
          }
      },
      //样本库添加角色 flagHandle
      async LibraryRoleHandle({row,rowIndex,$rowIndex,column,columnIndex,$columnIndex,cell},flagHandle)
      {
          //点击查看用户列不处理
          if (!column.property) {
              return
          }
          //获取需要添加的样本库ID与角色编号 xTable xTableRightDown  xTableRightTop
          try {
              let libraryIdSelectRow = this.$refs.xTable.getCurrentRow()
              let dictionaryRoleRow = this.$refs.xTableRightTop.getCurrentRow()
              let libraryRoleRow = this.$refs.xTableRightDown.getCurrentRow()
              let strMessage ;
              var canRequest = false;
              if (libraryIdSelectRow&&libraryIdSelectRow.librid) {
                  if (flagHandle=='delete') {
                      strMessage = "删除"
                      if (libraryRoleRow&&libraryRoleRow.roleno)
                      {
                          this.selroleRow.librid = libraryIdSelectRow.librid
                          this.selroleRow.roleno = libraryRoleRow.roleno
                          canRequest = true;
                      }
                  }
                  else if (flagHandle=='insert')
                  {
                      strMessage = "新增"
                      if (dictionaryRoleRow&&dictionaryRoleRow.roleno) {
                          this.selroleRow.librid = libraryIdSelectRow.librid
                          this.selroleRow.roleno = dictionaryRoleRow.roleno
                          canRequest = true;
                      }
                  }
              }
              if (canRequest) {
                  const res = await this.$tool.request({
                          url: "SMP/SMP1/SMP130",
                          method: "post",
                          data: {
                              action: "LibraryRoleHandle",
                              flagHandle:flagHandle,
                              data: this.selroleRow,
                          },
                      })
                  if (res) {
                          if (res.errcode=='1') {
                              this.rightTopTableData = res.obj.roleDictionary.Data;
                              this.rightDownTableData = res.obj.libraryRole.Data;
                              this.$message({
                              message: strMessage+"角色成功！",
                              type: "success",
                              showClose:true,
                          })
                          }
                          else{
                              this.$message({
                                  message: "样本库ID:"+res.keyid+strMessage+"失败!\r\n原因:"+res.obj,
                                  type: "error",
                                  showClose:true,
                              })
                          }
                  }
              }

          } catch (e) {
               this.$message({
                  message: e.message,
                  type: "warning",
                  showClose:true,
              })
          }
      },
      btnAdd() {
          this.actionindex = "A"
          this.changeFlag = true
          this.detailData = {
              librid: null,
              librcode: "",
              librname: "",
              librstatus: 0,
              librtype: null,
              librstatus: "0",
              dutyuser: "",
              telnum: "",
              createuser: this.$tool.getSession(this.$tool.loginuserno),
              createdt: "",
              librdesc: "",
          }
          this.$nextTick(() => {
              this.$refs.txtLibrcode.focus()
          })
      },
      btnDel() {
          if (this.detailData.librid == "") {
              this.$message({
                  message:"请先选择一条数据！",
                  type: "warning",
                  showClose:true,
              })
              return false
          }
          this.bDelVisible = true
      },
      btnDelCancel() {
          this.bDelVisible = false
          this.$message({
              type: "info",
              message: "已取消删除",
              showClose:true,
          })
      },
      btnDelComfirm() {
          this.bDelVisible = false
          let that = this
          this.$tool
              .request({
                  url: "SMP/SMP1/SMP130",
                  method: "post",
                  data: {
                      action: "smp_library_delete",
                      librid: this.librid,
                  },
              })
              .then((res) => {
                  if (res == "1") {
                      this.$message({
                          message: "删除成功！",
                          type: "success",
                          showClose:true,
                      })
                      this.actionindex = "M"
                      //是删除操作
                      var delrow = that.$refs.xTable.getCurrentRow()

                      if (delrow) {
                          var alld = that.$refs.xTable.getTableData().fullData
                          var rowindex = alld.indexOf(delrow)
                          that.$refs.xTable.remove(delrow)
                          setTimeout(async () => {
                              let havedata = that.$refs.xTable.tableData
                              if (havedata.length > 0) {
                                  if (rowindex >= havedata.length)
                                      rowindex = havedata.length - 1
                                  let selectrow = havedata[rowindex]
                                  that.$refs.xTable.setCurrentRow(selectrow)
                                  that.librid = selectrow.librid
                                  await this.DetailQuery()
                                  that.$refs.xTable.scrollToRow(selectrow)
                              }
                          }, 5)
                      }
                      this.ifCanEditFlag = false
                  } else {
                      this.$message({
                          message: "删除失败！" + res.msg,
                          type: "error",
                          showClose:true,
                      })
                  }
              })
              .catch((e) => {
                      this.$message({
                      message: e,
                      type: "error",
                      showClose:true,
                  })
              })
      },
      btnSave() {
          let that = this
          let gData = XEUtils.clone(that.detailData, true)
          if (this.actionindex == "A") {
              gData.createuser = this.$tool.getSession(this.$tool.loginuserno)
              gData.createdt = this.$pub.InitDateTime()
          } else if (this.actionindex == "M") {
              //无数据时
              if (this.librid == "") {
                  this.$alert(
                      "请通过点击新增来添加数据。",
                      "提示",
                      { type: "warning" },
                      {
                          confirmButtonText: "确定",
                      }
                  )
                  this.btnCancel()
                  return false
              }
          }
          if (gData.librcode == "") {
              this.$alert(
                  "样本库代码不可为空！",
                  "提示",
                  { type: "warning" },
                  {
                      confirmButtonText: "确定",
                  }
              )
              this.$refs.txtLibrcode.focus()
              return false
          }
          if (gData.librname == "") {
              this.$alert(
                  "样本库名称不可为空！",
                  "提示",
                  { type: "warning" },
                  {
                      confirmButtonText: "确定",
                  }
              )
              this.$refs.txtLibrName.focus()
              return false
          }
          if (gData.librid == "") {
              gData.librid = 0
          } else {
              gData.librid = parseInt(gData.librid, 10)
          }
          this.$tool
              .request({
                  url: "SMP/SMP1/SMP130",
                  method: "post",
                  data: {
                      action: "smp_library_addedit",
                      data: gData,
                  },
              })
              .then((res) => {
                  if (res.errcode == "1") {
                      that.$message({
                          message: "保存成功！",
                          type: "success",
                          showClose:true,
                      })
                      this.librid = res.keyid
                      this.changeFlag = false
                      //不是删除操作
                      if (this.actionindex == "A") {
                          gData.librid = this.librid
                          this.detailData.librid = this.librid
                          this.detailData.createdt = gData.createdt
                          if (gData.librstatus == "0") {
                              gData.librstatus = "在用"
                          } else if (gData.librstatus == "1") {
                              gData.librstatus = "停用"
                          }
                          this.$opt.sampleLibraryType.forEach((item) => {
                              if (gData.librtype == item.value)
                                  gData.librtype = item.label
                          })
                          this.$refs.xTable
                              .insertAt(gData, -1)
                              .then(({ row }) => {
                                  this.$refs.xTable.setCurrentRow(row)
                                  this.selRow = row
                              })
                          if (this.addData.indexOf(this.librid) < 0) {
                              this.addData.push(this.librid)
                          }
                      } else if (this.actionindex == "M") {
                          this.setToTable(gData)
                          if (this.modData.indexOf(this.librid) < 0) {
                              this.modData.push(this.librid)
                          }
                          var mrow = this.$refs.xTable.getCurrentRow()
                          this.$refs.xTable.setCurrentRow(mrow)
                      }
                      this.selRow = this.$refs.xTable.getCurrentRow()
                      this.actionindex = "M"
                      setTimeout(async () => {
                          if (this.selRow) {
                              await this.DetailQuery() //比较保存后值是否发生改变
                              this.$refs.xTable.setCurrentRow(this.selRow)
                              this.$refs.xTable.scrollToRow(this.selRow)
                          }
                      }, 5)
                  } else {
                      this.actionindex = that.actionindex
                      this.$message({
                          message: res.obj,
                          type: "error",
                          showClose:true,
                      })
                  }
              })
              .catch((e) => {
                  this.$message({
                      message: e,
                      type: "error",
                      showClose:true,
                  })
              })
      },
      async btnCancel() {
          this.actionindex = "M"
          this.selflag = "1"
          this.changeFlag = false
          let row = this.$refs.xTable.getCurrentRow()
          if (row) {
              this.librid = row.librid
          }
          await this.btnQuery()
      },
      btnExport() {},
      /*-------------------------------其它-------------------------------*/
      //获取样本类型
      ddlLibrtype() {
          this.$tool
              .request({
                  url: "SMP/SMP1/SMP130",
                  method: "post",
                  data: {
                      action: "get_librtype",
                  },
              })
              .then((res) => {
                  $opt.sampleLibraryType = res.Data
                  this.$opt.sampleLibraryType.forEach((item) => {
                      item.codeno = parseInt(item.codeno)
                  })
              })
      },
      Autofocus(element) {
          if (element == "txtLibrcode") {
              this.$refs.txtLibrcode.blur()
              this.$refs.txtLibrName.focus()
          } else if (element == "txtLibrName") {
              this.$refs.selLibrType.focus()
          } else if (element == "selLibrType") {
              this.$refs.selLibrType.blur()
              this.$refs.txtDutyuser.focus()
          } else if (element == "txtDutyuser") {
              this.$refs.txtTelnum.focus()
          } else if (element == "txtTelnum") {
              this.$refs.txtLibrdesc.focus()
          } else if (element == "txtLibrdesc") {
              this.btnSave()
          }
      },
      async setToTable(obj) {
          this.$nextTick(() => {
              this.selRow.librid = obj.librid
              this.selRow.librcode = obj.librcode
              this.selRow.librname = obj.librname

              if (obj.librstatus == "0") {
                  this.selRow.librstatus = "在用"
              } else if (obj.librstatus == "1") {
                  this.selRow.librstatus = "停用"
              }
              // this.selRow.librtype = obj.librtype
              this.$opt.sampleLibraryType.forEach((item) => {
                  if (obj.librtype == item.value)
                      this.selRow.librtype = item.label
              })
              this.selRow.telnum = obj.telnum
              this.selRow.dutyuser = obj.dutyuser
              this.selRow.librdesc = obj.librdesc
          })
      },
      /*------------------------------Class------------------------------*/
      CellClassName({ columnIndex, row }) {
          if (columnIndex == "0" || columnIndex == "1") {
              if (row.flag == "1") {
                  //不能再次选择
                  return "col-orange"
              } else {
                  //新增的数据
                  return ""
              }
          } else {
              return ""
          }
      },
      RowClassName({ row, rowIndex }) {
          if (row.librstatus == "停用") {
              return "rm-alert"
          } else {
              ;("")
          }
          if (this.addData.indexOf(row.librid) > -1) {
              return "row-new"
          }
          if (this.modData.indexOf(row.librid) > -1) {
              return "row-modify"
          }
      },
      /*用户弹窗 */
      //数据筛选
      filterdata() {
          if (this.firstUser) {
              this.firstUserTableData = this.userTableData

          }
          this.firstUser = false;
          let cData = this.firstUserTableData;
          try {
              if (this.qcode != "") {
                  console.log(this.qcode);
                  cData = cData.filter((item) => item.Userno.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 || item.UserName.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1 );
              }
              else
              {
                  cData = this.firstUserTableData;
              }
              if (cData.length > 0) {
                  this.selrow = cData[0];
              } else {
                  this.selrow = {};
              }

              this.userTableData = cData;
          }
           catch (e) {
              console.log("erro", e);
          }
      },
      //弹框过滤input输入事件
      autosearch(evt) {
      if (evt.keyCode != 13 && evt.keyCode != 40 && evt.keyCode != 38) {
          this.filterdata();
          }
      },
      searchkeydown(evt) {

      },
      btnok() {
          this.dialogRoleUserTableVisible = false;
          this.qcode = '';
      },
      //弹窗刷新数据按钮
      async refreshdata() {
          this.qcode = '';
          const res = await this.$tool.request({
                  url: "SMP/SMP1/SMP130",
                  method: "post",
                  data: {
                      action: "QueryRoleIdUser",
                      roleId: this.refreshFlag,
                  },
              })
          if (res) {
              //数据源
              if (res.Data) {
                  this.userTableData = res.Data
              }
              //绑定列
              if (res.Cols) {
                  var collist = [];
                      for (let i = 0, len = res.Cols.length; i < len; i++) {
                          const item = res.Cols[i];
                          if (item.field != "hidkey" && item.field != "hidname" && item.field != "inputcode" && item.field != "shortmsgcode") {
                              let colitem = {
                                  field: item.field,
                                  title: item.title,
                                  width: item.width,
                              };
                          collist.push(colitem);
                      }
                  }
                  this.userTableColumn = collist;
              }
          }
      },
  },
}
</script>

<style scoped>
.pagetop {
  width: 100%;
  height: 35px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  font-size: 12px;
  z-index: 99;
  overflow-x: auto;
  white-space: nowrap;
}
.top-deal {
  right: 5px;
  position: absolute;
  top: 5px;
  display: flex;
  align-items: center;
}
.search-title {
  margin: 0 5px 0 20px;
  font-size: 12px;
}
.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}

.middle-center {
  position: absolute;
  height: 100%;
  left: 0px;
  right: 455px;
  overflow: auto;
}

.middle-right {
  position: absolute;
  height: 100%;
  width: 450px;
  right: 0px;
}
table {
  font-size: 12px;
  border-collapse: separate;
  border-spacing: 5px;
}
::v-deep .el-input.is-disabled .el-input__inner {
  color: #606266;
  cursor: not-allowed;
}
::v-deep  .vxe-table .vxe-body--column.col-orange {
  background-color: #f8f8f8;
}
::v-deep .el-input--suffix .el-input__inner {
  padding-right: 5px;
}
::v-deep  tr.vxe-body--row.row-new {
  color: #cc6600;
}
::v-deep  tr.vxe-body--row.row-modify {
  color: blue !important;
}
::v-deep .el-textarea__inner {
  padding: 5px;
  cursor: default;
}
</style>
